/*
* @Author: Rosen
* @Date:   2017-05-18 19:30:12
* @Last Modified by:   Rosen
* @Last Modified time: 2017-05-27 19:46:42
*/

'use strict';
require('./index.css');
var _mm     = require('util/mm.js');
// 通用页面头部
var headerName = {
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        var name = _mm.getUrlParam('name');
        // keyword存在，则回填输入框
        if(name){
            $('#search-input').val(name);
        };
    },
    bindEvent : function(){
        var _this = this;
        // 点击搜索按钮以后，做搜索提交
        $('#search-btn').click(function(){
            _this.searchSubmit();
        });
        // 输入会车后，做搜索提交
        $('#search-btn').keyup(function(e){
            // 13是回车键的keyCode
            if(e.keyCode === 13){
                _this.searchSubmit();
            }
        });
     /*   $("#search-input").bind("input propertychange",function(event){
            console.log("dfdsf")
            console.log($("#search-input").val())
        });*/
        var flag = false;
        var t= this;
        $("#search-input").on('input', function () {
            $(".ul").show();
            if (!flag)
                t.sereach($(this).val())
        }).on('compositionstart', function () {
            flag = true;
        }).on('compositionend', function () {
            flag = false;
            t.sereach($(this).val())
            console.log('输入法，输入结束--'+$(this).val());
        });
        $(".search-li").click(function () {
            debugger
            var v = $(this).innerHTML
        });
        $("#search-input").blur(function () {
            $(".ul").hide();
        })
        $("#search-input").click(function () {
            $(".ul").show();
        })
    },
    sereach:function(value) {
    $.ajax({
        type: 'get',
        url: _mm.getServerUrl("/hotel/searchSuggestInfo"),
        dataType: 'json',
        data: {key: value},
        async: false,
        success: function (res) {
            console.log(res)
            // 请求成功
            if (res.status == true) {
                var list =res.entity;
                var html="";
                $(".ul").find("li").remove()
                if(list.length > 0){
                    for(var i=0,j=list.length;i<j;i++){
                        html+='<li class="search-li" >'+
                            '<span class="search-item">'+list[i]+'</span>'+
                            '</li>'
                    }
                    console.log(html)
                    $(".ul").append(html)
                }
            }
            // 没有登录状态，需要强制登录
            else if (10 === res.status) {

            }
            // 请求数据错误
            else if (1 === res.status) {

            }
        },
        error: function (err) {

        }
    });
},


    // 搜索的提交
    searchSubmit : function(){
        var name = $.trim($('#search-input').val());
        // name,正常跳转到listHotelName页
        if(name){
            window.location.href = './listAutomaticPrompt.html?name=' + name;
        }
        // 如果keyword为空，直接返回首页
        else{
            window.location.href = './listAutomaticPrompt.html';
        }
    }
};

headerName.init();